<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>会员列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="{{URL::asset("layui/css/layui.css")}}"  media="all">
</head>
<body>
<fieldset class="layui-elem-field" style="margin-top: 20px;">
    <legend>搜索查询</legend>
    <form class="layui-form" action="">

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">昵称</label>
                <div class="layui-input-inline">
                    <input type="text" name="nickname" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">姓名</label>
                <div class="layui-input-inline">
                    <input type="text" name="username" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">电话</label>
                <div class="layui-input-inline">
                    <input type="text" name="phone" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-input-inline">
                    <button class="layui-btn" lay-submit="" lay-filter="demo1">搜索</button>
                </div>
            </div>
        </div>
    </form>
</fieldset>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-group">
        <button class="layui-btn layui-btn-primary layui-btn-sm" lay-event="add"><i class="layui-icon layui-icon-add-1"></i>新增</button>
    </div>
</script>

<table class="layui-hide" id="test" lay-filter="test"></table>



<script src="{{URL::asset('layui/layui.js')}}" charset="utf-8"></script>
<script src="{{URL::asset('js/jquery-3.1.1.min.js')}}"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->

<script>
    layui.use(['table','form'], function(){
        var table = layui.table,
        form = layui.form;

        table.render({
            elem: '#test'
            ,url:'/admin/member/api/list'
            ,toolbar: '#toolbarDemo'
            ,title: '用户数据表'
            ,cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field:'id', title:'ID', width:80, fixed: 'left', unresize: true, sort: true}
                ,{field:'avatar', title:'头像', width:120, templet:function (d) {
                        return '<img src="'+d.avatar+'" width="50px" height="50px" />';
                    }}
                ,{field:'nickname', title:'昵称'}
                ,{field:'username', title:'姓名'}
                ,{field:'phone', title:'电话'}
                ,{field:'sell_id', title:'销售ID'}
                ,{field:'parent_id', title:'邀请人ID'}
                ,{field:'score', title:'积分'}
                ,{field:'recharge', title:'充值金额',templet:function (d) {
                        return '￥'+d.recharge;
                    }}
                ,{field:'user_type', title:'身份',templet:function (d) {
                        if (d.user_type==1){
                            return '<span class="layui-badge-rim">普通用户</span>';
                        }else if(d.user_type==2){
                            return '<span class="layui-badge layui-bg-blue">销售用户</span>';
                        }else if(d.user_type==3){
                            return '<span class="layui-badge layui-bg-black">代理用户</span>';
                        }else{
                            return '<span class="layui-badge layui-bg-orange">假用户</span>';
                        }
                    }}
                ,{fixed: 'right', title:'操作', width:200,templet:function (d) {
                        var jd = '';
                        if (d.user_type!=2){
                            jd += '<a class="layui-btn layui-btn-xs" lay-event="set">设为销售</a><a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="score">增加积分</a>';
                        }else{
                            jd += '<a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="score">增加积分</a>';
                        // <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                        }
                        return jd;
                    }}
            ]]
            ,page: true
        });

        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'add':
                    layer.open({
                        type:2,
                        title:"新增用户",
                        area:["50%","40%"],
                        content:'/admin/member/html/add'
                    });
                    break;
            };
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            //console.log(obj)
            if(obj.event === 'del'){
                layer.confirm('该操作将永久删除此数据,是否确认?', function(index){
                    $.post('/admin/member/api/delete',{"id":data.id},function (d) {
                        if (d.code==200){
                            setTimeout(function () {
                                obj.del();
                                layer.close(index);
                            },500)
                        } else {
                            layer.msg("删除失败");
                        }
                    });
                });
            } else if(obj.event === 'set'){
                layer.confirm('是否确认将此用户设为销售?', function(index){
                    $.post('/admin/member/api/sell',{"id":data.id},function (d) {
                        if (d.code==200){
                            layer.msg("设置成功");
                            setTimeout(function () {
                                table.reload('test');
                            },500)
                        } else {
                            layer.msg("设置失败");
                        }
                    });
                });
            } else if (obj.event === 'score'){
                layer.open({
                    title:"增加积分",
                    content:'<input type="number" id="score" placeholder="请输入积分值" autocomplete="off" class="layui-input">',
                    btn:["确定","取消"],
                    yes:function () {
                        var score = $('#score').val();
                        $.post('/admin/member/score/add',{"id":data.id,"score":score},function (d) {
                            if (d.code==200){
                                // layer.open({
                                //     title:"温馨提示",
                                //     content:"是否进入积分管理?",
                                //     btn:["确定","取消"],
                                //     yes:function (index) {
                                //         var l = parent === self ? layui : top.layui;
                                //         l.index.openTabsPage('/admin/score/html', '积分列表');
                                //         layer.close(index)
                                //     }
                                // })
                                layer.msg("积分申请已提交审核!");
                            } else{
                                layer.msg("积分增加失败");
                            }
                        });
                    }
                });
            }
        });

        //监听提交
        form.on('submit(demo1)', function(data){
            var param = data.field;
            //执行重载
            table.reload('test', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                ,where: {
                    nickname: param.nickname,
                    phone:param.phone,
                    username:param.username,
                }
            });
            return false;
        });
    });
</script>

</body>
</html>